<?php
            
use app\model\Theme; 
use app\model\Poster; 
use app\model\Worker; 



/**
 * web页面的接口，同时需要使用框架模版引擎
 * 尽量不要在php代码里嵌套html或者js代码，看着让人头疼
 * html模版中js使用jquery或原生。。。。。。慢慢就会适应。。。。。。
 */
class Index_Page extends PluginWebPage {

    /**
     * Desc:
     * User:wp
     * Email:1565602678@qq.com
     * Time: 2022/5/19 4:44 下午
     * @param $method
     */
    function main($method)
    {
        $method = 'index_main';
        $do = 'weike';
        global $_G,$_P,$_S,$_C;
        $system = $_S;
        $config = $_C;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        $themelist = Theme::getAllTheme();
        $page_index = max(1,intval($_P['page']));
        $page_size = 50;
        $page_start = ($page_index-1)*$page_size;
        $search_data = isset($_P['search_data'])?$_P['search_data']:'';
        if ($search_data){
            $search_data = '%'.$search_data.'%';
        }
        $wk_list = WeikeModel::getWeikeList($themeid,$search_data,$page_start,$page_size);
        $count = WeikeModel::getWeikeList($themeid,$search_data,0,0);
        $total = $page_total = $count['count_num'];
        $list = [];
//        var_dump(json_decode($wk_list[9]['config'],true));die();
        foreach ($wk_list as $item){
            $wk_config = json_decode($item['config'],true);
            $item['left_days'] = '已结束';
            $left_days = (strtotime($wk_config['end_time'])-strtotime(date('Y-m-d')))/(3600*24);
            if($left_days > 0){
                $item['left_days'] = $left_days;
            }
            $item['config'] = $wk_config;
            $item['case_gift_desc'] = '未兑换';
            if($item['has_case_gift']){
                //查找兑换gift_name
                $item['case_gift_desc'] = '已兑换：'.$wk_config['case_gift_name'];
            }
            $list[] = $item;
        }
        //查询好友达标数量
        $limit_num = $config['wk_friend_limit_num']?:10;//标准默认暂时为10
        $count_limit_num = WeikeModel::getWeikeFriendLimitNum($themeid,$limit_num);
        $pager = pagination($page_total,$page_index,$page_size);
        WeikeModel::editWkRecordInfo($themeid,$config);
        $return_clicent = get_defined_vars();
return $this->view('/weike/index/main', $return_clicent);
    }

    function addpage(){
        $method = 'add_weike';
        $do = 'weike';
        global $_G,$_P,$_S,$_C;
        $system = $_S;
        $config = $_C;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        $wk_id = intval($_P['wk_id'])?:0;
        $data = [];
        $wk_config = [];
        $wk_url = '';
        $wlist = Worker::getworkerlist($system,$themeid);
        $workerid = 0;
        $wkUserInfo = [];
        $music_src = '';
        $music_id = 0;
        $music_list = [];
        $call_list = [];
        $gift_list = [];
        if($wk_id){
            $method = 'edit_weike';
            $data = WeikeModel::getWkInfoById($wk_id,$themeid);
            $wk_config = $data['config'];
            $wk_url = $wk_config['wk_url']?:'';
            if(!$wk_url){
                //请求生成小程序码，参数待定；
                $theme = Theme::getThemeById($themeid);
                $scene = MODULE_NAME . "-" . $themeid . "-w" . $wk_id . "-" .$theme['softkey']. "-" . 0;
                $qrcode = Poster::CreateScene($system,$scene,$config,'weike/pages/weike/weike');
                $wk_config['wk_url'] = $qrcode;
                $wk_url = $qrcode;
                WeikeModel::editWkDataByWhere(['id'=>$wk_id],['config'=>json_encode($wk_config)]);
            }
            $workerid = $wk_config['workerid']?:0;
            $wkUserInfo = db_get(t_fans, ['themeid' => $themeid, 'uid' => $data['uid']]);
            $music_src = $wk_config['music_src']?:'';
            $music_id = $wk_config['music_id']?:0;
            $music_list = WeikeModel::getMusicList($themeid);
            $call_list = WeikeModel::getCallRecordList($themeid,$wk_id);
            $gift_list = WeikeModel::getGiftRecordList($themeid,$wk_id);
        }
        //查找礼品组
        $wk_call_gift_groups = WeikeModel::getCallGiftGroups($themeid,false);
        //查找礼物组
        $wk_gift_groups = WeikeModel::getGiftGroups($themeid,false);
        //小程序码待定，应该第一次生成后存入数据库中，后边直接进行取字段
        $return_clicent = get_defined_vars();
return $this->view('/weike/index/edit', $return_clicent);
    }

    function addinfo(){
        global $_G,$_P,$_S,$_C;
        $system = $_S;
        $config = $_C;
        $wk_id = intval($_P['wk_id']);
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        if (!$themeid){
            show_json(0,'活动入口不正确');
            return;
        }
        $name = trim($_P['name'])?:'';
        $show_type = intval($_P['show_type'])?:0;
        if (empty($name)){
            show_json(0,'姓名不能为空！');
            return;
        }
        if (empty($_P['photo'])){
            show_json(0,'请上传照片！');
            return;
        }
        //处理图片
        $photos = [];
        foreach ($_P['photo'] as $v){
            $photos[]=tomedia($v);
        }
        $share_img = $_P['share_img']?tomedia($_P['share_img']):'';
        $workerid = $_P['workerid']?:0;
        if(!$workerid){
            show_json(0,'请选择员工！');
            return;
        }
        $music_id = $_P['music_id']?:0;
        $music_src = $_P['music_src']?:'';
        if($wk_id){
            //编辑
            //根据ID查找信息
            $info = WeikeModel::getWkInfoById($wk_id,$themeid);
            if(empty($info)){
                show_json(0,'未找到客片信息');
                return;
            }
            //姓名、打call数、礼物数、金币数、图片数组、展示类型、开始时间、结束时间、礼物分组ID、礼品分组ID
            //音乐url
            $data = [
                'name'=>$name,
                'call_num'=>intval($_P['call_num'])?:0,
                'gift_num'=>intval($_P['gift_num'])?:0,
                'money'=>floatval($_P['money'])?:0,
            ];
            $old_config = $info['config'];
            $call_gift_group_id = intval($_P['wk_call_gift_group_id']);
            if ($call_gift_group_id == 0){
                $call_gift_group_id = $old_config['wk_call_gift_group_id'];
            }
            $wk_gift_group_id = intval($_P['wk_gift_group_id']);
            if ($wk_gift_group_id == 0){
                $wk_gift_group_id = $old_config['wk_gift_group_id'];
            }
            if ($_P['call_time']['end'] < $_P['call_time']['start']){
                show_json(0,'结束时间不能小于开始时间');
                return;
            }
            $wk_config = $old_config;
            $wk_config['photo'] = $photos;
            $wk_config['show_type'] = $show_type;
            $wk_config['start_time'] = $_P['call_time']['start'];
            $wk_config['end_time'] = $_P['call_time']['end'];
            $wk_config['wk_call_gift_group_id'] = $call_gift_group_id;
            $wk_config['wk_gift_group_id'] = $wk_gift_group_id;
            $wk_config['shareimg'] = $share_img;
            $wk_config['workerid'] = $workerid;
            $wk_config['music_id'] = $music_id;
            $wk_config['music_src'] = $music_src;
            $data['config'] = json_encode($wk_config);
            $where = ['id'=>$wk_id,'themeid'=>$themeid];
            //修改数据库字段
            $res = WeikeModel::editWkDataByWhere($where,$data);
            if (!$res){
                show_json(0,['message'=>'操作失败']);
                return;
            }else{
                show_json(1,['message'=>'操作成功','wk_id'=>$wk_id]);
                return;
            }
        }else{
            //新增
            //查找config中配置天数，计算开始时间和结束时间
            $days = $config['wk_call_days']?:10;
            $start_time = date('Y-m-d');
            $end_time = date('Y-m-d',strtotime("+ $days days"));
            if ($end_time < $start_time){
                show_json(0,'结束时间不能小于开始时间');
                return;
            }
            //礼物和礼品分组ID
            $call_gift_group_id = intval($_P['wk_call_gift_group_id'])?:$config['wk_call_gift_group_id'];
            if ($call_gift_group_id == 0){
                //查找数据，取值最新的
                $call_gift_groups = WeikeModel::getCallGiftGroups($themeid);
                if (!empty($call_gift_groups)){
                    $call_gift_group_id = $call_gift_groups['id'];
                }
            }
            $wk_gift_group_id = intval($_P['wk_gift_group_id'])?:$config['wk_gift_group_id'];
            if ($wk_gift_group_id == 0){
                //查找数据，取值最新的
                $gift_groups = WeikeModel::getGiftGroups($themeid);
                if (!empty($gift_groups)){
                    $wk_gift_group_id = $gift_groups['id'];
                }
            }
            //随机取音乐
            $music_list = WeikeModel::getMusicList($themeid);
            if(!empty($music_list)){
                $index = array_rand($music_list);
                $music_id = $music_list[$index]['id'];
                $music_src = $music_list[$index]['url'];
            }
            //show_type 取值config
            $show_type = isset($config['wk_photo_show_type'])?intval($config['wk_photo_show_type']):0;
            //拼接数组
            $data = [
                'themeid'=>$themeid,
                'name'=>$name,
                'call_num'=>0,
                'gift_num'=>0,
                'money'=>0,
                'page_view'=>0,
                'delete'=>0,
            ];
            $wk_config = [
                'photo'=>$photos,
                'show_type'=>$show_type,
                'start_time'=>$start_time,
                'end_time'=>$end_time,
                'wk_call_gift_group_id'=>$call_gift_group_id,
                'wk_gift_group_id'=>$wk_gift_group_id,
                'arecord_ids'=>array(),//报名支付成功ID数组暂定为空数组
                'shareimg'=>$share_img,
                'workerid'=>$workerid,
                'music_id'=>$music_id,
                'music_src'=>$music_src,
            ];
            $data['config'] = json_encode($wk_config);
            //存入数据库
            $res = WeikeModel::insertWkData($data);
            if (!$res){
                show_json(0,['message'=>'操作失败']);
                return;
            }else{
                show_json(1,['message'=>'操作成功','wk_id'=>$res]);
                return;
            }
        }
    }

    function removeBind(){
        global $_G,$_P,$_S,$_C;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        $wk_id = intval($_P['wk_id']);
        if (!$wk_id){
            show_json(0,'参数错误');
            return;
        }
        $wkInfo = WeikeModel::getWkInfoById($wk_id,$themeid);
        if (empty($wkInfo)){
            show_json(0,'未找到微客信息');
            return;
        }
        if (!$wkInfo['uid']){
            show_json(0,'微客未绑定！');
            return;
        }
        $data['uid'] = 0;
        $where = ['id'=>$wk_id,'themeid'=>$themeid];
        //修改数据库字段
        $res = WeikeModel::editWkDataByWhere($where,$data);
        if (!$res){
            show_json(0,['message'=>'操作失败']);
            return;
        }else{
            show_json(1,['message'=>'操作成功']);
            return;
        }
    }

    function deleteInfo(){
        global $_G,$_P,$_S,$_C;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        $wk_id = intval($_P['wk_id']);
        if (!$wk_id){
            show_json(0,'参数错误');
            return;
        }
        $wkInfo = WeikeModel::getWkInfoById($wk_id,$themeid);
        if (empty($wkInfo)){
            show_json(0,'未找到微客信息');
            return;
        }
        if ($wkInfo['delete']){
            show_json(0,'微客已删除！');
            return;
        }
        $data['delete'] = 1;
        $where = ['id'=>$wk_id,'themeid'=>$themeid];
        //修改数据库字段
        $res = WeikeModel::editWkDataByWhere($where,$data);
        if (!$res){
            show_json(0,['message'=>'操作失败']);
            return;
        }else{
            show_json(1,['message'=>'操作成功']);
            return;
        }
    }

    function caselistinfo(){
        $method = 'case_gift_list';
        $do = 'weike';
        global $_G,$_P,$_S,$_C;
        $system = $_S;
        $config = $_C;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        $themelist = Theme::getAllTheme();
        $page_index = max(1,intval($_P['page']));
        $page_size = 50;
        $page_start = ($page_index-1)*$page_size;
        $search_data = isset($_P['search_data'])?$_P['search_data']:'';
        if ($search_data){
            $search_data = '%'.$search_data.'%';
        }
        $wk_list = WeikeModel::getWeikeList($themeid,$search_data,$page_start,$page_size);
        $count = WeikeModel::getWeikeList($themeid,$search_data,0,0);
        $total = $page_total = $count['count_num'];
        $list = [];
        foreach ($wk_list as &$item){
            $wk_config = json_decode($item['config'],true);
            $item['left_days'] = '已结束';
            $left_days = (strtotime($wk_config['end_time'])-strtotime(date('Y-m-d')))/(3600*24);
            if($left_days > 0){
                $item['left_days'] = $left_days;
            }

            $item['config'] = $wk_config;
            $can_case_gift = '暂无';
            if($item['call_num'] > 0){
                //查找groupid下礼品
                $gift_lists = WeikeModel::getCallGiftListByWhere($wk_config['wk_call_gift_group_id'],$item['call_num']);
                if(!empty($gift_lists)){
                    $name_list = array_column($gift_lists,'title');
                    $can_case_gift = implode("，",$name_list);
                }
            }
            $item['can_case_gift'] = $can_case_gift;
            $list[] = $item;
        }
        $pager = pagination($page_total,$page_index,$page_size);
        $return_clicent = get_defined_vars();
return $this->view('/weike/index/casegift', $return_clicent);
    }

    function collectinfolist(){
        $method = 'collect_info_list';
        $do = 'weike';
        global $_G,$_P,$_S,$_C;
        $system = $_S;
        $config = $_C;
        $themeid = intval($_P['themeid']);
        $wk_id = intval($_P['wk_id'])?:0;
        $collect_type = trim($_P['collect_type'])?:'';
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        $themelist = Theme::getAllTheme();
        $page_index = max(1,intval($_P['page']));
        $page_size = 50;
        $page_start = ($page_index-1)*$page_size;
        $search_data = isset($_P['search_data'])?$_P['search_data']:'';
        if ($search_data){
            $search_data = '%'.$search_data.'%';
        }
        $list = WeikeModel::getCollectList($themeid,$wk_id,$collect_type,$search_data,$page_start,$page_size);
        $count = WeikeModel::getCollectList($themeid,$wk_id,$collect_type,$search_data,0,0);
        $total = $page_total = $count['count_num'];
        $pager = pagination($page_total,$page_index,$page_size);
        $return_clicent = get_defined_vars();
return $this->view('/weike/index/collectlist', $return_clicent);
    }

    function resetCode(){
        global $_G,$_P,$_S,$_C;
        $system = $_S;
        $config = $_C;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        $wk_id = intval($_P['wk_id']);
        if (!$wk_id){
            show_json(0,'参数错误');
            return;
        }
        $wkInfo = WeikeModel::getWkInfoById($wk_id,$themeid);
        if (empty($wkInfo)){
            show_json(0,'未找到微客信息');
            return;
        }
        //重新生成小程序码并更新
        $wk_config = $wkInfo['config'];
        //请求生成小程序码，参数待定；
        $theme = Theme::getThemeById($themeid);
        $scene = MODULE_NAME . "-" . $themeid . "-w" . $wk_id . "-" .$theme['softkey']. "-" . 0;
        $qrcode = Poster::CreateScene($system,$scene,$config,'weike/pages/weike/weike');
        $wk_config['wk_url'] = $qrcode;
        //修改数据库字段
        $res = WeikeModel::editWkDataByWhere(['id'=>$wk_id,'themeid'=>$themeid],['config'=>json_encode($wk_config)]);
        if (!$res){
            show_json(0,['message'=>'操作失败']);
        }else{
            show_json(1,['message'=>'操作成功']);
        }
    }

}